<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="国家分类名称" prop="nationSortName">
        <el-select v-model="queryParams.nationSortName" class="!w-240px" placeholder="请选择国家分类名称" style="width: 240px"
          @keyup.enter="handleQuery">
          <el-option v-for="item in nationSortNameList" :key="item.id" :label="item.categoryName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
          start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-240px" />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['repository:video:create']">
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" align="center" prop="id" :width="60" />
      <el-table-column label="封面" align="center" prop="coverSite">
        <template #default="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.coverSite" :zoom-rate="1.2" :max-scale="7"
            :min-scale="0.2" :preview-src-list="srcList" :initial-index="4" fit="cover" />
        </template>
      </el-table-column>
      <el-table-column label="视频" align="center" prop="site" :width="200">
        <template #default="scope">
          <video-player @click="handleVideo(scope.row)" v-if="scope.row.site"
            class="video-player vjs-big-play-centered videoBox" :src="scope.row.site" poster="" crossorigin="Anonymous"
            controls playsinline :volume="0.6" :playback-rates="[0.7, 1.0, 1.5, 2.0]" :height="90" />
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="name" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="来源" align="center" prop="source" />
      <el-table-column label="场景分类" align="center" prop="nationSortName" />
      <el-table-column label="领域分类" align="center" prop="readSortName" />
      <el-table-column label="关键字" align="center" prop="keywords" />
      <el-table-column label="简介" align="center" prop="description" :width="110" />
      <el-table-column label="操作" align="center" min-width="110" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['repository:video:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['repository:video:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <VideoForm ref="formRef" @success="getList" />
  <videoDialog ref="videoDialogRef" :dialogTitle="dialogTitle" :currentSource_src="currentSource_src" />
</template>
<script lang="ts" setup>
import download from '@/utils/download'
import * as VideoApi from '@/api/repository/video'
import VideoForm from './VideoForm.vue'
import * as CategoryApi from '@/api/repository/category'

//资源管理-视频
defineOptions({ name: 'RepositoryVideo' })

const videoDialogRef = ref()
const dialogTitle = ref('视频播放')
const currentSource_src = ref('')

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据

const nationSortNameList = ref([]) // 国家分类名称列表
const readSortNameList = ref([]) // 特定场景阅读分类名称列表
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  nationSortName: undefined,
  createTime: []
})
const handleVideo = (data) => {
  currentSource_src.value = data.site
  videoDialogRef.value.open('打开dialog')
}
const getNationSortNameList = async () => {
  const data = await CategoryApi.getCategoryList(1)
  nationSortNameList.value = data
}
const getReadSortNameList = async () => {
  const data = await CategoryApi.getCategoryList(2)
  readSortNameList.value = data
}
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await VideoApi.getVideoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id, nationSortNameList.value, readSortNameList.value)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await VideoApi.deleteVideo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

const selectedItems = ref([])
/** 多选 */
const handleSelectionChange = (val) => {
  selectedItems.value = val
}
/** 导出按钮操作 */
const handleExport = async () => {
  if (selectedItems.value.length == 0 || selectedItems.value.length > 10) {
    ElMessage.warning('请选择要下载的数据，不能同时超过10条!')
    return
  }
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    let ids = []
    selectedItems.value.forEach((res) => {
      ids.push(res.id)
    })
    const params = {
      ids: ids.join(';')
    }
    message.success(t('正在导出数据，请稍后。'))
    const data = await VideoApi.exportVideo(params)
    download.zip(data, 'gf.zip')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  await getNationSortNameList()
  await getReadSortNameList()
})
</script>
